package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.SpannableStringUtils;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.ThirdPartyInfo;
import com.rongshu.view.userInfo.iview.RealAuthenticationView;
import com.rongshu.view.userInfo.presenter.RealAuthenticationPresenter;
import com.rongshu.view.web.LocalWebActivity;
import com.widget.EditTextWithDel;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class RealAuthenticationActivity extends BaseActivity<RealAuthenticationPresenter> implements RealAuthenticationView {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;//标题
    @BindView(R.id.txt_authentication_agreement)
    TextView txt_authentication_agreement;
    @BindView(R.id.layout_real_authentication)
    LinearLayout layout_real_authentication;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;
    @BindView(R.id.edt_name)
    EditTextWithDel edt_name;
    @BindView(R.id.edt_id_card)
    EditTextWithDel edt_id_card;

    //map
    private Map<String, Object> map = null;

    @Override
    protected int initLayout() {
        return R.layout.activity_real_authentication_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new RealAuthenticationPresenter(this), this);
        //设置title
        txt_top_title.setText(R.string.setting_real_authentication);
        //富文本显示
        txt_authentication_agreement.setText(SpannableStringUtils.addForeColorSpan(txt_authentication_agreement.getText().toString(), getResources().getColor(R.color.color_3cc051), 11, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //用户协议
                Intent intent = new Intent(RealAuthenticationActivity.this, LocalWebActivity.class);
                intent.putExtra(Constant.INTENT_H5, Common.PRIVACY_POLICY);
                intent.putExtra(Constant.INTENT_TITLE, getString(R.string.privacy_policy));
                startActivity(intent);
            }
        }));
        txt_authentication_agreement.setMovementMethod(LinkMovementMethod.getInstance());
        txt_authentication_agreement.setHighlightColor(getResources().getColor(android.R.color.transparent));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {
        getThirdParty();
    }

    /**
     * 获取第三方账号绑定信息和实名认证信息
     */
    private void getThirdParty() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getThirdParty(user_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 实名认证
     */
    private void getRealNameAuthentication(String id_card, String name) {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("id_card", id_card);
        map.put("name", name);
        presenter.getRealNameAuthentication(user_id, id_card, name, Common.encryptionSign(map));
        map.clear();
    }


    @OnClick({R.id.layout_back, R.id.txt_start_authentication})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                setResult(RESULT_OK);
                finish();
                break;
            case R.id.txt_start_authentication://开始认证
                String name = edt_name.getText().toString().trim();
                String id_card = edt_id_card.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    toast("请输入真实姓名");
                    return;
                }
                if (TextUtils.isEmpty(id_card)) {
                    toast("请输入身份证号");
                    return;
                }
                getRealNameAuthentication(id_card, name);
                break;
        }
    }

    /**
     * 获取第三方账号绑定信息和实名认证信息
     */
    @Override
    public void onThirdPartyInfo(ThirdPartyInfo info) {
        if (info.getErrCode() == 0) {
            //实名
            String name = info.getResponse().getName();
            //id
            String id_card = info.getResponse().getId_card();
            if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(id_card)) {
                layout_real_authentication.setVisibility(View.GONE);
                layout_empty.setVisibility(View.VISIBLE);
                //设置title
                txt_top_title.setText("审核认证");
            } else {
                layout_real_authentication.setVisibility(View.VISIBLE);
                layout_empty.setVisibility(View.GONE);
                //设置title
                txt_top_title.setText(R.string.setting_real_authentication);
            }
        } else {
            toast(info.getErrMsg());
        }
    }


    /**
     * 实名认证
     */
    @Override
    public void onRealAuthentication(INetBean bean) {
        if (bean.getErrCode() == 0) {
            toast("提交成功");
            showKeyboard(false);
            setResult(RESULT_OK);
            finish();
        } else {
            toast(bean.getErrMsg());
        }
    }
}
